<template>
  <div>
    <el-dialog
      :model-value="open"
      :close-on-click-modal="false"
      :title="title"
      width="500px"
      append-to-body
      :before-close="handleClose"
    >
      <el-form
        ref="editForm"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="form.dictName" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="form.dictType" placeholder="请输入字典类型" />
        </el-form-item>
        <el-form-item label="数据类型" prop="dataType">
          <el-select
            v-model="form.dataType"
            :disabled="dataTypeDisabled"
            filterable
            placeholder="请选择数据类型"
            clearable
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dicts[DictDirection.是否禁用字典]" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance, watch, defineExpose, toRaw, reactive, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import { getType, addType, updateType } from '@/api/system/dict'
import useDicts, { DictDirection } from '@/store/modules/dicts'

const props = defineProps<{
  open,
  title,
  dataTypeDisabled
}>()
const emit = defineEmits(['changeList', 'update:open', 'update:title'])
const dicts = useDicts()
// 表单参数
const form = ref({
    dictId: undefined,
    dictName: undefined,
    dictType: undefined,
    status: undefined,
    remark: undefined,
    dataType: undefined,
  }),
  // 表单校验
  rules = {
    dictName: [{ required: true, message: '字典名称不能为空', trigger: 'blur' }],
    dictType: [{ required: true, message: '字典类型不能为空', trigger: 'blur' }],
  },
  // 新增修改表单
  editForm = ref(null),
  {
    appContext: {
      config: {
        globalProperties: { $resetForm, $validateForm },
      },
    },
  } = getCurrentInstance()

// 重置新增修改列表
const reset = () => {
  form.value = {
    dictId: undefined,
    dictName: undefined,
    dictType: undefined,
    status: '0',
    remark: undefined,
    dataType: undefined,
  }
  $resetForm(editForm.value)
}
const options = [
  {
    value: 0,
    label: '数值',
  },
  {
    value: 1,
    label: '字符串',
  },
]
// 新增修改——确定
const submitForm = () => {
  $validateForm(editForm.value, () => {
    let msg = ''
    let response = null
    if (form.value.dictId != undefined) {
      msg = '修改成功'
      response = updateType(form.value)
      emit('update:open', false)
    } else {
      msg = '新增成功'
      response = addType(form.value)
      emit('update:open', false)
    }
    response
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success(msg)
        emit('update:open', false)
        emit('changeList')
      } else {
        ElMessage.error(res.msg)
      }
    })
    .catch((error) => {
      ElMessage.error(error)
      emit('update:open', false)
    })
  })
}
// 修改新增——取消
const cancel = () => {
  emit('update:open', false)
}
// 根据id查询字典类型
const getList = (id) => {
  getType(id)
  .then((res) => {
    if (res.code === 200) {
      const dictList = JSON.parse(JSON.stringify(res.data))
      form.value = dictList[0]
    } else {
      ElMessage.error(res.msg)
    }
  })
  .catch((err) => {
    console.log(err)
    ElMessage.error(err)
  })
}
dicts.setDicts(DictDirection.是否禁用字典)
const handleClose = () => {
  emit('update:open', false)
  reset()
}
defineExpose({
  getList,
})
</script>
